package com.xyht.sca_s.student_manage_system.modules.stampRegistration.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.entity.dto.SmsStampRegistrationAllowDto;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.entity.dto.SmsStampRegistrationCancelDto;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.entity.dto.SmsStampRegistrationDto;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.entity.dto.SmsStampRegistrationHistoryReq;
import com.xyht.sca_s.student_manage_system.modules.stampRegistration.service.SmsStampRegistrationService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-05-25
 */
@RestController
@SecretBody
@RequestMapping("/stampRegistration")
public class SmsStampRegistrationController {

    @Resource
    private SmsStampRegistrationService smsStampRegistrationService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("提交申请")
    @PostMapping("/add")
    public ResponseResult addStampRegistration(HttpServletRequest request, @RequestBody @Validated SmsStampRegistrationDto stampRegistrationDto){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsStampRegistrationService.addStampRegistration(userId,stampRegistrationDto);
    }

    @ApiModelProperty("撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelStampRegistration(HttpServletRequest request, @RequestBody SmsStampRegistrationCancelDto cancelDto){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsStampRegistrationService.cancelStampRegistration(userId,cancelDto);
    }

    @ApiModelProperty("审批申请")
    @PostMapping("/allow")
    public ResponseResult allowStampRegistration(HttpServletRequest request, @RequestBody SmsStampRegistrationAllowDto allowDto){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsStampRegistrationService.allowStampRegistration(userId,allowDto);
    }

    @ApiModelProperty("申请人指定流程(提交前获取)")
    @GetMapping("/processList")
    public ResponseResult getStampOriginSettingProcess(){
        return smsStampRegistrationService.getStampOriginSettingProcess();
    }

    @ApiModelProperty("获取审批详情")
    @GetMapping("/detail")
    public ResponseResult getStampRegistrationDetail(HttpServletRequest request,
                                                     @RequestParam(value = "examineId",required = false) String examineId){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsStampRegistrationService.getStampRegistrationDetail(userId,examineId);
    }

    @ApiModelProperty("获取审批列表")
    @GetMapping("/list")
    public ResponseResult getStampRegistrationList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "examine_type",required = false) Integer examine_type,
                                                   @RequestParam(value = "search_str",required = false) String search_str,
                                                   HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsStampRegistrationService.getStampRegistrationList(userId,examine_type,pageNum,pageSize,search_str);
    }

    @ApiModelProperty(value = "申请表下载")
    @GetMapping("/download")
    public void tableDownload(@RequestParam(value = "apply_id") String apply_id, HttpServletResponse response) {

        try{
            Future<ResponseResult> future = smsStampRegistrationService.tableDownload(apply_id, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();

        }


    }


    @ApiModelProperty("查看所有记录")
    @GetMapping("/allList")
    public ResponseResult getAllStampRegistrationList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "allowStatus",required = false) Integer allowStatus,
                                                      @RequestParam(value = "applyName",required = false) String applyName){
        return smsStampRegistrationService.getAllStampRegistrationList(allowStatus,applyName,pageNum,pageSize);
    }

    @ApiModelProperty(value = "导出历史记录")
    @PostMapping("/exportHistory")
    public ResponseResult exportExamineHistoryList(@RequestBody SmsStampRegistrationHistoryReq req, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsStampRegistrationService.exportExamineHistoryList(req, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
}

